package com.senseauto.mediamuxerdemo;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.Range;
import android.widget.RelativeLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;

import java.util.ArrayList;
import java.util.List;

/**
 * 主要用来申请权限
 */
public class MainActivity extends AppCompatActivity {
    String TAG = "MainActivity";
    private String[] permissions = new String[]{Manifest.permission.CAMERA,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.RECORD_AUDIO};
    private List<String> mPermissionList = new ArrayList<>();
    private int PERMISSION_REQUEST_CODE = 100;

    private boolean hasAllPermission() {
        mPermissionList.clear();
        if (Build.VERSION.SDK_INT < 23) {
            return true;
        }
        for (int i = 0; i < permissions.length; i++) {
            if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
                mPermissionList.add(permissions[i]);
            }
        }
        return mPermissionList.size() <= 0;

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean hasPermissionDismiss = false;
        if (requestCode == PERMISSION_REQUEST_CODE) {
            for (int i = 0; i < grantResults.length; i++) {
                if (grantResults[i] == -1) {
                    hasPermissionDismiss = true;
                }
            }

            if (hasPermissionDismiss) {
                Toast.makeText(this, "请授予必要权限", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(!hasAllPermission()){
             if(Build.VERSION.SDK_INT>=23){
                requestPermissions(permissions, PERMISSION_REQUEST_CODE);
            }
        }
        findViewById(R.id.btnGo).setOnClickListener(v->{
            //startActivity(new Intent(this, RecordActivity.class));
            startActivity(new Intent(this, MediaCodecActivity.class));
        });
        Log.e(TAG, "输出编码器信息看看 count:"+MediaCodecList.getCodecCount());
        // 遍历所有可用的编解码器
        for (int i = 0; i < MediaCodecList.getCodecCount(); i++) {
            MediaCodecInfo codecInfo = MediaCodecList.getCodecInfoAt(i);
            // 1. 只找编码器，跳过解码器
            if (!codecInfo.isEncoder()) {
                continue;
            }

            // 2. 按需要的MIME类型过滤（例如H.264）
            String[] types = codecInfo.getSupportedTypes();
            for (String type : types) {
                if (type.equalsIgnoreCase("video/avc") ) { // 对于H.265，则是 "video/hevc"
                    Log.e(TAG, "进来了");
                    MediaCodecInfo.CodecCapabilities capabilities = codecInfo.getCapabilitiesForType(type);

                    // 3. 获取视频编码能力对象
                    MediaCodecInfo.VideoCapabilities vidCaps = capabilities.getVideoCapabilities();

                    // 4. 查询支持的宽高范围、对齐要求等
                    Range<Integer> widthRange = vidCaps.getSupportedWidths();
                    Range<Integer> heightRange = vidCaps.getSupportedHeights();
                    int widthAlignment = vidCaps.getWidthAlignment();
                    int heightAlignment = vidCaps.getHeightAlignment();

                    Log.e(TAG, "Encoder: " + codecInfo.getName());
                    Log.e(TAG, "Supported widths: " + widthRange.toString());
                    Log.e(TAG, "Supported heights: " + heightRange.toString());
                    Log.e(TAG, "Width alignment: " + widthAlignment);
                    Log.e(TAG, "Height alignment: " + heightAlignment);

                    // 你还可以检查特定尺寸是否支持
                    boolean is1080pSupported = vidCaps.isSizeSupported(1920, 1080);
                    Log.e(TAG, "Is 1080p supported: " + is1080pSupported);
                }
            }
        }
    }
}